package chapter9;

import java.util.Arrays;

public class Test9_6_1 {
    public static void main(String[] args) {
        //方法1
        System.out.println("sum=" + getSum(2,32));

        //方法2
        System.out.println("sum=" + getSum2());
    }

    /**
     * 获取2-32随机6个偶数之和
     * @param num1
     * @param num2
     * @return
     */
    public static int getSum(int num1, int num2){
        int[] arr = new int[6];
        int sum = 0;
        for(int i=0;i<arr.length;i++){
            int num = num1 + (int)(Math.random()*(num2-num1));
            if(num%2 == 0){
                arr[i] = num;
            }else {
                arr[i] = num-1;
            }
            System.out.print(arr[i] + " ");
            sum += arr[i];
        }
        return sum;
    }

    public static int getSum2(){
        int[] arr ={} ;
        int index = 0;
        for(int i = 2; i < 32; i++){
            if(i%2 == 0){
                arr = Arrays.copyOf(arr,index+1);
                arr[index] = i;
                index ++;
            }
        }
        for (int i : arr) {
            System.out.print(i+" ");
        }
        System.out.println();
        int[] arr1 = new int[6];
        for(int i=0;i<arr1.length;i++){
            index = (int)(Math.random()*arr.length);
            arr1[i] = arr[index];
        }
        int sum = 0;
        for (int i : arr1) {
            System.out.print(i+" ");
            sum += i;
        }
        System.out.println();
        return sum;
    }
}
